<template>
  <div class="seck-list">
    <template v-if="$route.path == '/home/secklist'">
      <el-row>
        <el-button type="primary" @click="handleaAdd">添加秒杀活动</el-button>
      </el-row>
      <el-table
        :data="seckList"
        stripe
        style="width: 100%"
        row-key="id"
        :tree-props="{ hasChildren: 'hasChildren', children: 'children' }">
<!--        编号-->
        <el-table-column
          prop="id"
          label="商品编号"
          width="80">
        </el-table-column>
<!--        名称title-->
        <el-table-column
          prop="title"
          label="商品名称"
          width="120">
        </el-table-column>
        <!--        开始时间-->
        <el-table-column
          label="开始时间"
          width="150">
          <template slot-scope="item">
            {{item.row.begintime | formatDate}}
          </template>
        </el-table-column>
        <!--        结束时间-->
        <el-table-column
          label="结束时间"
          width="150">
          <template slot-scope="item">
            {{item.row.endtime | formatDate}}
          </template>
        </el-table-column>
<!--        一级分类id-->
        <el-table-column
          label="一级分类">
          <template slot-scope="item">
            {{first_category(item.row.first_cateid)}}
<!--            -->
          </template>
        </el-table-column>
        <!--       二级分类id-->
        <el-table-column
          label="二级分类">
          <template slot-scope="item">
            {{second_category(item.row)}}
          </template>
        </el-table-column>
        <!--        商品id-->
        <el-table-column
          label="商品id">
          <template slot-scope="item">
            {{item.row.goodsid}}
          </template>
        </el-table-column>
<!--        状态-->
        <el-table-column
          prop="status"
          label="状态">
          <template slot-scope="item">
            <el-switch
              :value="item.row.status"
              active-color="#f00"
              inactive-color="#eee"
              :active-value="1"
              :inactive-value="2">
            </el-switch>
          </template>
        </el-table-column>
        <!--      操作-->
        <el-table-column width="200" label="操作">
          <template slot-scope="item">
            <el-button type="success" icon="el-icon-edit" size="mini" @click="editItem(item.row.id)">修改</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteItem(item.row.id)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
<!--    {{categoryList}}-->
    <el-row>
      <router-view></router-view>
    </el-row>

  </div>
</template>

<script>
  import {mapState, mapActions} from 'vuex'
  import {formatDate} from '@/util/util.js';
  export default {
    name: "vSeckList",
    computed: {
      ...mapState(['seckList','categoryList'])
    },
    filters: {
      formatDate(time) {
        var date = new Date(Number(time));//Mon Jan 19 1970 01:28:27 GMT+0800 (中国标准时间)
        return formatDate(date, 'yyyy-MM-dd hh:mm');
      }
    },
    created() {
      this.updateseck()
      this.updatecategory()//请求一级分类
    },
    data() {
      return {

      }
    },
    methods: {
      //过滤器方法
      first_category(value) {
        let currentCate = this.categoryList.filter( item1 => {return item1.id == value})
        return currentCate[0].catename
      },
      second_category(value) {
        console.log(value,'-----------------')
        let currentCate = this.categoryList.filter( item1 => {return item1.id == value.first_cateid})
        return currentCate[0].children.filter(item2 => {return item2.id == value.second_cateid})[0].catename
      },
      ...mapActions(['updateseck','updatecategory']),
      //进入添加商品页面
      handleaAdd() {
        this.$router.push('/home/secklist/seckadd')
      },
      //删除功能
      deleteItem(id) {
        this.$confirm('您正在删除活动选项, 是否继续?', '提示', {
          cancelButtonText: '取消',
          confirmButtonText: '确定',
          type: 'warning'
        }).then(() => {
          this.axios({
            method: 'post',
            url: '/api/seckdelete',
            data: {id: id}
          }).then(res => {
            this.updateseck()
            this.$message({
              type: 'success',
              message: res.msg
            });
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      //修改菜单信息功能
      editItem(id) {
        this.$router.push('/home/secklist/seckedit/' + id)
      }
    }
  }
</script>

<style scoped>

</style>
